#include "client.h"

void * recvFilefunc(void * arg){
    srfile_t * srfile = (srfile_t *)arg;
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = srfile->addr.sin_family;
    addr.sin_port = srfile->addr.sin_port;
    addr.sin_addr.s_addr = srfile->addr.sin_addr.s_addr;
    int ret = connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");

    train_t train;
    train.length = srfile->train.length;
    strcpy(train.token,srfile->train.token);
    strcpy(train.buf,srfile->train.buf);
    int length = 2*sizeof(int)+sizeof(train.token)+train.length;
    send(sockfd,&length,sizeof(int),MSG_NOSIGNAL);
    send(sockfd,&train,sizeof(train_t),MSG_NOSIGNAL);

    length = recvfile(sockfd,train.buf);
    if(length == 0){
        printf("Gets success\n");
        fflush(stdout);
    }else{
        printf("Gets failed,please try again\n");
        fflush(stdout);
    }
    pthread_exit(NULL);
}
